package com.star.truffle.shop.controller;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import com.star.framework.autoconfigure.HnServiceException;
import com.star.framework.autoconfigure.web.BaseResult;
import com.star.truffle.shop.constants.ErrorCodeEnum;
import com.star.truffle.shop.dto.req.ShopSaveReqeustDto;
import com.star.truffle.shop.dto.res.ShopListResponseDto;
import com.star.truffle.shop.service.ShopService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@Tag(name = "店铺")
@RestController
public class ShopController {
  @Autowired
  private ShopService shopService;
  
  @PostMapping("/api/auth/shop/add")
  @Operation(method = "POST", summary = "新增店铺")
  public BaseResult<Long> addShop(
      @Parameter(description = "头部信息，用户id") @RequestHeader(name = "userId", required = false) @NotNull(message = "请登录") Long userId,
      @Valid @RequestBody ShopSaveReqeustDto saveDto) {
    try {
      Long id = shopService.addShop(userId, saveDto);
      return BaseResult.success(id);
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
    }
  }
  
  @PostMapping("/api/auth/shop/list")
  @Operation(method = "POST", summary = "店铺列表")
  public BaseResult<List<ShopListResponseDto>> listShop() {
    try {
      List<ShopListResponseDto> list = shopService.listShop();
      return BaseResult.success(list);
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
    }
  }
}
